/**
 *  @file
 *  @brief   槽（C）形（CHAN）梁截面属性使用数值方法计算类
 *  @author  Li Zhaoni
 *  @date    2024.07.22
 *  @version v1.0
 *  @par Copyright(c):  ShenGong  Simulation corporation
 */

#include "ChanSectionNumericalCalculator.h"

#include "DataStructure/Common/Real.h"

#include "SectionCalculator/StandardSectionTool.h"

using SG::Algebra::Point;
using SG::DataStructure::Common::Real;
using SG::DataStructure::FEM::PBARLData;
using SG::FEMSolver::Element::ChanSectionNumericalCalculator;

void ChanSectionNumericalCalculator::Mesh (const PBARLData& section, _OUT Meshes& meshes, int refineLevel) const
{
    Real w1{ section.m_dimension[0] };
    Real w2{ w1 };
    Real w3{ section.m_dimension[1] };
    Real t1{ section.m_dimension[3] };
    Real t2{ t1 };
    Real t3{ section.m_dimension[2] };
    Real wf1{ w1 - t3 };
    Real wf2{ w2 - t3 };
    Real hw{ w3 - t1 - t2 };

    int n{ refineLevel + 1 };
    int nwf1{ 2 * n };
    int nt1{ n };
    int nwf2{ 2 * n };
    int nt2{ n };
    int nhw{ 2 * n };
    int nt3{ n };

    StandardSectionTool secMesh;
    Point               point;
    int                 nodeId{ 0 };
    int                 eleId{ 0 };
    Meshes              newMeshes;
    NodeSet             newEdgeNodes;
    int                 numRepeatNodes{ 0 };
    NodeSet             edgeNodes;

    // 1st block
    point.m_y = w2;
    point.m_z = w3 - t2;
    secMesh.MeshRect (point, -wf2, t2, nwf2, nt2, nodeId, eleId, _OUT newMeshes, _OUT newEdgeNodes);
    secMesh.Merge (edgeNodes, numRepeatNodes, newMeshes, meshes, nodeId, eleId);
    secMesh.UpdateEdgeNodes (newEdgeNodes, numRepeatNodes, edgeNodes);

    // 2nd block
    point.m_y = 0.0;
    secMesh.MeshRect (point, t3, t2, nt3, nt2, nodeId, eleId, _OUT newMeshes, _OUT newEdgeNodes);
    numRepeatNodes = 2 * nt2 + 1;
    secMesh.Merge (edgeNodes, numRepeatNodes, newMeshes, meshes, nodeId, eleId);
    secMesh.UpdateEdgeNodes (newEdgeNodes, numRepeatNodes, edgeNodes);

    // 3rd block
    point.m_z = t1;
    secMesh.MeshRect2 (point, t3, hw, nt3, nhw, nodeId, eleId, _OUT newMeshes, _OUT newEdgeNodes);
    numRepeatNodes = 2 * nt3 + 1;
    secMesh.Merge (edgeNodes, numRepeatNodes, newMeshes, meshes, nodeId, eleId);
    secMesh.UpdateEdgeNodes (newEdgeNodes, numRepeatNodes, edgeNodes);

    // 4th block
    point.m_z = 0.0;
    secMesh.MeshRect2 (point, t3, t1, nt3, nt1, nodeId, eleId, _OUT newMeshes, _OUT newEdgeNodes);
    numRepeatNodes = 2 * nt3 + 1;
    secMesh.Merge (edgeNodes, numRepeatNodes, newMeshes, meshes, nodeId, eleId);
    secMesh.UpdateEdgeNodes (newEdgeNodes, numRepeatNodes, edgeNodes);

    // 5th block
    point.m_y = w1;
    secMesh.MeshRect (point, -wf1, t1, nwf1, nt1, nodeId, eleId, _OUT newMeshes, _OUT newEdgeNodes);
    numRepeatNodes = 2 * nt1 + 1;
    secMesh.Merge (edgeNodes, numRepeatNodes, newMeshes, meshes, nodeId, eleId);
}